
#include "s3c24x0.h"
#include "s3c2440.h"
#include "2440addr.h"
#include "stdio.h"




extern void IsrUsbd(void);
extern void IsrDma2(void);

/************************* Timer ********************************/
static int intCount;

void ClearPending_my(int bit) 
{
	S3C24X0_INTERRUPT * const intregs = S3C24X0_GetBase_INTERRUPT();
	intregs->SRCPND= bit;
	//myprintf("intregs->srcpnd = %x\n",intregs->srcpnd);
	//	myprintf("intregs->srcpnd = %x\n",&intregs->srcpnd);
	intregs->INTPND= bit;
	//myprintf("intregs->intpnd = %x\n",intregs->intpnd);
	//myprintf("intregs->intpnd = %x\n",&intregs->intpnd);
}
void Timer_InitEx(void)
{
	S3C24X0_INTERRUPT * const intregs = S3C24X0_GetBase_INTERRUPT();
	intCount=0;	
	intregs->SUBSRCPND= (1<<13);
	ClearPending_my(BIT_WDT_AC97/*BIT_WDT*/);
	intregs->INTMSK&=~(BIT_WDT_AC97 /*BIT_WDT*/);
	intregs->INTSUBMSK&= ~(1<<13);
}



void Timer_StartEx(void)
{
	S3C24X0_WATCHDOG * const wdtregs = S3C24X0_GetBase_WATCHDOG();

	wdtregs->WTCON=((50000000/1000000-1)<<8)|(0<<3)|(1<<2);	// 16us
	wdtregs->WTDAT=0xffff;
	wdtregs->WTCNT=0xffff;   

	// 1/16/(65+1),interrupt enable,reset disable,watchdog enable
	wdtregs->WTCON=((50000000/1000000-1)<<8)|(0<<3)|(1<<2)|(0<<0)|(1<<5);   
}

unsigned int Timer_StopEx(void)
{
	int count;
	S3C24X0_WATCHDOG * const wdtregs = S3C24X0_GetBase_WATCHDOG();
	S3C24X0_INTERRUPT * intregs = S3C24X0_GetBase_INTERRUPT();

	wdtregs->WTCON=((50000000/1000000-1)<<8);
	intregs->INTMSK|=BIT_WDT_AC97; //BIT_WDT;
	intregs->INTSUBMSK|= (1<<13);

	count=(0xffff-wdtregs->WTCNT)+(intCount*0xffff);
	return ((unsigned int)count*16/1000000);
}


void  IsrWatchdog(void)
{
	S3C24X0_INTERRUPT * const intregs = S3C24X0_GetBase_INTERRUPT();
	//printf("watchdog\n");
	intregs->SUBSRCPND= (1<<13);
	ClearPending_my(BIT_WDT_AC97 /* BIT_WDT */);
	intCount++;   	
}
#if 0
int g_TimerIntHappen;
void  IsrTimer4(void)
{
	myprintf("timer4\n");
	ClearPending_my(BIT_TIMER4);
	*(volatile int *)&g_TimerIntHappen = 1;
}
#endif


void Isr_Init(void)
{
	
	//pISR_TIMER4= (unsigned int)IsrTimer4;
	pISR_WDT_AC97= (unsigned int)IsrWatchdog;




#ifdef CONFIG_USB_DEVICE
	pISR_USBD= (unsigned int)IsrUsbd;
	pISR_DMA2= (unsigned int)IsrDma2;

#endif    

}


